﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace OPSYS_Schematic.Model
{
    public class GisSymbol : FrameworkElement
    {

        public static readonly DependencyProperty DrawingSymbolProperty =
           DependencyProperty.Register("DrawingSymbol",
           typeof(Drawing),
           typeof(GisSymbol),
           new PropertyMetadata(new PropertyChangedCallback(ChangeSymbolDrawingCallBack))
           );

        public static readonly RoutedEvent SymbolClickEvent = EventManager.RegisterRoutedEvent(
            "SymbolClick",
            RoutingStrategy.Tunnel,
            typeof(RoutedEventHandler),
            typeof(GisSymbol)
            );


        public Drawing DrawingSymbol
        {
            get { return (Drawing)GetValue(DrawingSymbolProperty); }
            set { SetValue(DrawingSymbolProperty, value); }
        }



        private VisualCollection _visualCollection;
        private DrawingVisual dv = new DrawingVisual();

        public GisSymbol()
        {
            _visualCollection = new VisualCollection(this);
            _visualCollection.Add(dv);
            //AddVisualChild(this);
            //AddLogicalChild(this);
        }


        static void ChangeSymbolDrawingCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Drawing drawing = (Drawing)e.NewValue;
            if (drawing == null) return;

            var gisSymbol = d as GisSymbol;

            using (DrawingContext dc = gisSymbol.dv.RenderOpen())
            {

                dc.DrawDrawing(drawing);

            }

        }


        public event RoutedEventHandler SymbolClick
        {

            add { AddHandler(SymbolClickEvent, value); }
            remove { RemoveHandler(SymbolClickEvent, value); }
        }
        void OnSymbolClick()
        {

            //SymbolClickRoutedEventArgs e = new SymbolClickRoutedEventArgs(SymbolClickEvent, this);
            RoutedEventArgs e = new RoutedEventArgs(SymbolClickEvent, this);
            RaiseEvent(e);
        }

        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            OnSymbolClick();
        }
        protected override Visual GetVisualChild(int index)
        {
            return _visualCollection[index];
        }
        protected override int VisualChildrenCount
        {
            get { return _visualCollection.Count; }
        }
    }


    public delegate void SymbolClickHandle(object sender, SymbolClickRoutedEventArgs e);

    public class SymbolClickRoutedEventArgs : RoutedEventArgs
    {
        public SymbolClickRoutedEventArgs(RoutedEvent routedEvent, object source)
            : base(routedEvent, source)
        {

        }
        protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
        {
            var handler = (EventHandler<SymbolClickRoutedEventArgs>)genericHandler;

            handler(genericTarget, this);
        }
    }
}
